package top.kiterunner.learning.concurrency_test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/*
我们要学会使用AtomicXXX原子类来完成一些基本的原子计数等操作。
*/

/**
 * AdorynZhao in Tencent-UESTC.
 * Source Code Created on 2017/7/6.
 */
public class ThreadDemo9 {

    public AtomicInteger count = new AtomicInteger(0);

    public void method() {
        for (int i = 0; i < 10000; i++) {
            count.incrementAndGet();
        }
    }

    public static void main(String[] args) {
        ThreadDemo9 t9 = new ThreadDemo9();

        List<Thread> threadList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            threadList.add(new Thread(new Runnable() {
                @Override
                public void run() {
                    t9.method();
                }
            }, "Thread" + i));
        }

        for (Thread t : threadList) {
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("Count = " + t9.count);
    }
}
